package com.gupao.ldr.pattern.singleton.lazy;

/**
 * 懒汉式(线程不安全)
 */
public class LazyDoubleCheckSingleton {

    private static LazyDoubleCheckSingleton lazyDoubleCheckSingleton;


    private LazyDoubleCheckSingleton() {

    }

    //JDK1.6之后对synchronized性能优化不少
    //不可避免存在一定性能的问题
    //双重检查锁
    public static LazyDoubleCheckSingleton getInstance() {


        /**
         * 如果把synchronized放在if外面，每次校验都要上锁，
         * 严重影响效率，所以使用双重判断来解决线程问题
         */
        if (lazyDoubleCheckSingleton == null) {
            synchronized(LazyDoubleCheckSingleton.class){
                if (lazyDoubleCheckSingleton==null)
                lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
            }
        }

        return lazyDoubleCheckSingleton;
    }

}
